package com.mysite.seckill.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysite.seckill.commons.common.Constant;
import com.mysite.seckill.commons.po.SeckillActivity;
import com.mysite.seckill.commons.po.User;
import com.mysite.seckill.commons.vo.DataTableResultVo;
import com.mysite.seckill.commons.vo.ResultVo;
import com.mysite.seckill.web.service.SeckillActivityService;

@Controller
@RequestMapping("/activity")
public class SeckillActivityController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SeckillActivityService seckillService;
    
    @RequestMapping("")
    public String index(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute(Constant.CURRENT_USER);
        model.addAttribute("loginedUser", user);
        return "activity";
    }
    
    @RequestMapping("/list")
    @ResponseBody
    public DataTableResultVo activities(String productName, String draw, String start, String length) {
        Map<String, Object> searchMap = new HashMap<String, Object>();
        searchMap.put("productName", productName);
        searchMap.put("draw", draw);
        searchMap.put("start", start);
        searchMap.put("length", length);
        return seckillService.getActivities(searchMap);
    }

    @RequestMapping("/create")
    @ResponseBody
    public ResultVo create(SeckillActivity activity) {
        return new ResultVo(seckillService.create(activity));
    }

    @RequestMapping("/join")
    @ResponseBody
    public ResultVo join(HttpSession session, String sn, int count) {
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        return new ResultVo(seckillService.join(user.getId(), sn, count));
    }
}
